package com.huarui.company.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.ewaf.framework.action.BaseAction;
import org.ewaf.framework.util.HttpUtil;

import com.huarui.company.model.Company;
import com.huarui.company.service.CompanyService;
import com.huarui.user.model.User;
import com.huarui.user.service.UserService;

/**
 * xzz
 * 
 * @author Administrator
 * 
 */
public class ValidateAction extends BaseAction {

	private Logger logger = Logger.getLogger(ValidateAction.class);
	private static final long serialVersionUID = 848351587714084194L;
	private UserService userService;
	private CompanyService companyService;
	private String userEmail;
	private String companyEmail;
	private String companyTaxCode;
	private String companyName;
	private String companyCode;

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setCompanyService(CompanyService companyService) {
		this.companyService = companyService;
	}

	public void setUserEmail(String userEmail) {
		this.userEmail = userEmail;
	}

	public void setCompanyEmail(String companyEmail) {
		this.companyEmail = companyEmail;
	}

	public void setCompanyTaxCode(String companyTaxCode) {
		this.companyTaxCode = companyTaxCode;
	}

	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}

	public void setCompanyCode(String companyCode) {
		this.companyCode = companyCode;
	}

	/**
	 * 验证公司创建者是否为系统注册用户
	 * 
	 * @return
	 * @throws Exception
	 */
	public String validateCreator() throws Exception {

		User user = userService.getUserByEmail(userEmail);
		PrintWriter out = grantWrite();

		if (user != null) {

			if (user.getCompanyId() != 0) {
				out.write("is already a company,can't regester");

			} else if ("0".equals(user.getStatus())) {

				out.write("please go to active ,and regester");

			} else {

				out
						.write("you are already regester user,please write you password and company message");

			}
		
		}
		
		return null;
	}

	/**
	 * 验证公司注册邮箱唯一性
	 * 
	 * @return
	 * @throws Exception
	 */
	public String validateCompanyEmail() throws Exception {
		System.out.println(companyEmail);
		Company company = companyService.getCompanyByEmail(companyEmail);
		PrintWriter out = grantWrite();
		if (company != null) {

			out.write("sorry，the company email have been used");
		}

		return null;
	}

	/**
	 * 验证公司税号唯一性
	 * 
	 * @return
	 * @throws Exception
	 */
	public String validateCompanyTaxCode() throws Exception {
		Company company = companyService.getCompanyByTaxCode(companyTaxCode,null);
		PrintWriter out = grantWrite();
		if (company != null) {

			out.write("sorry,the taxCode have been used");
		}

		return null;
	}

	/**
	 * 验证公司名称唯一性
	 * 
	 * @return
	 * @throws Exception
	 */
	public String validateCompanyName() throws Exception {

		Company company = companyService.getCompanyByName(companyName);

		PrintWriter out = grantWrite();
		
		if (company != null) {

			out.write("sorry!the company name  have been used ");
		}

		return null;
	}

	/**
	 * 验证公司机构代码唯一性
	 * 
	 * @return
	 * @throws Exception
	 */
	public String validateCompanyCode() throws Exception {
		Company company = companyService.getCompanyByCode(companyCode);
		System.out.println(company);
		PrintWriter out = grantWrite();
		if (company != null) {

			out.write("sorry，the company code already have been used");
		}

		return null;
	}

	private PrintWriter grantWrite() {
		HttpServletResponse response = HttpUtil.getResponse();
		httpServletResponse.setContentType("text/plain; charset=UTF-8"); // 处理中文问题的必须的代码
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {

			logger.debug(e);
		}
		return out;
	}
}
